from email.mime.text import MIMEText
from email.header import Header
import smtplib

# title为标题
# website为网址
# boxes为邮箱列表


def mail(title, website, boxes):
    host_server = 'smtp.qq.com'
    sender_qq = '2913623528'
    pwd = 'byrfvsihcxhydghb'
    # 发件人的邮箱
    sender_qq_mail = '%s@qq.com' % sender_qq
    # 邮件的正文内容
    mail_content = '<p><a href = "%s">%s</a></p>' % (website, website)
    # 邮件标题
    mail_title = title

    # 登录
    smtp = smtplib.SMTP(host_server)
    smtp.starttls()  # 建立安全连接
    smtp.set_debuglevel(1)
    smtp.ehlo(host_server)
    smtp.login(sender_qq, pwd)

    for r in boxes:
        msg = MIMEText(mail_content, 'html', 'utf-8')
        msg["Subject"] = Header(mail_title, 'utf-8').encode()
        msg["From"] = sender_qq_mail
        msg["To"] = Header(r, 'utf-8').encode()
        smtp.sendmail(sender_qq_mail, r, msg.as_string())

    smtp.quit()


if __name__ == "__main__":
    try:
        title = "hi"
        website = "www.baidu.com"
        boxes = ["1005121848@qq.com"]
        mail(title, website, boxes)
        print("邮件发送成功")
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")